package com.dick.base.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dick.base.mapper.BaseDepartmentMapper;
import com.dick.base.mapper.BaseUserDepartmentMapper;
import com.dick.base.model.BaseDepartment;
import com.dick.base.model.BaseUserDepartment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.LinkedList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@Service
public class DepartmentService {

    @Autowired
    private BaseUserDepartmentMapper baseUserDepartmentMapper;
    @Autowired
    private BaseDepartmentMapper baseDepartmentMapper;


    /**
     * 获取某用户任职的所有部门
     * @param userId
     * @return
     */
    public List<BaseDepartment> getDepartmentsByUserId(Long userId) {
        List<BaseDepartment> departments = new LinkedList<>();
        BaseUserDepartment baseUserDepartmentSelect = new BaseUserDepartment();
        baseUserDepartmentSelect.setUserId(userId);
        List<BaseUserDepartment> userDepartments = baseUserDepartmentMapper.selectList(new QueryWrapper<>(baseUserDepartmentSelect));
        if (!userDepartments.isEmpty()) {
            departments.addAll(baseDepartmentMapper.selectBatchIds(userDepartments.stream().map(u -> u.getDepartmentId()).collect(Collectors.toList())));
        }
        return departments;
    }

    /**
     * 把用户移入某部门
     * @param useId
     * @param departmentId
     */
    public void moveIn(Long useId, Integer departmentId) {
        BaseUserDepartment baseUserDepartment = new BaseUserDepartment();
        baseUserDepartment.setUserId(useId);
        baseUserDepartment.setDepartmentId(departmentId);
        if (baseUserDepartmentMapper.selectCount(new QueryWrapper<>(baseUserDepartment)) == 0) {
            baseUserDepartmentMapper.insert(baseUserDepartment);
        }
    }

    /**
     * 把用户移出某部门
     * @param useId
     * @param departmentId
     */
    public void moveOut(Long useId, Integer departmentId) {
        BaseUserDepartment baseUserDepartment = new BaseUserDepartment();
        baseUserDepartment.setUserId(useId);
        baseUserDepartment.setDepartmentId(departmentId);
        baseUserDepartmentMapper.delete(new QueryWrapper<>(baseUserDepartment));
    }

    /**
     * 把用户任职部门列表转为部门类型集合
     * @return
     */
    public Set<String> getDepartmentTypesByDepartments(List<BaseDepartment> departments) {
        return departments.stream().map(d -> d.getDepartmentType()).collect(Collectors.toSet());
    }

    /**
     * 把用户任职部门列表转为部门id集合
     * @return
     */
    public Set<Integer> getDepartmentIdsByDepartments(List<BaseDepartment> departments) {
        return departments.stream().map(d -> d.getId()).collect(Collectors.toSet());
    }

    /**
     * 查询所有特定类型的部门
     * @param departmentType
     * @return
     */
    public List<BaseDepartment> getDepartmentsByType(String departmentType) {
        return baseDepartmentMapper.selectList(new QueryWrapper<BaseDepartment>().eq("department_type", departmentType));
    }

    /**
     * 查询部门名称
     * @param id
     * @return
     */
    public String getDepartmentNameById(Integer id) {
        return baseDepartmentMapper.selectById(id).getName();
    }
}
